package com.xtecher.app.user.ui;

import android.graphics.Color;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.widget.DefaultItemAnimator;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.githang.statusbar.StatusBarCompat;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.xtecher.app.R;
import com.xtecher.app.base.BaseActivity;
import com.xtecher.app.user.FollowUtils;
import com.xtecher.app.user.UserContact;
import com.xtecher.app.user.UserPresenter;
import com.xtecher.app.user.adapter.FollowAdapter;
import com.xtecher.app.user.bean.FollowUserResponse;
import com.xtecher.app.utils.StatusBarUtil;

import java.lang.reflect.Type;
import java.util.ArrayList;
import java.util.List;

public class FollowActivity extends BaseActivity implements UserContact.View {

    private TextView tvTitle;
    private View viewDivider;
    private ImageView imgClose;
    private RecyclerView reFollow;
    private UserPresenter mPresenter;
    private List<FollowUserResponse> responses;
    private FollowAdapter followAdapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_follow);
        StatusBarCompat.setStatusBarColor(this, Color.parseColor("#0ce9ecf2"));
        StatusBarUtil.StatusBarLightMode(this);
        initView();
        mPresenter = new UserPresenter(this);
        initListener();
    }

    @Override
    protected void onResume() {
        super.onResume();
        String follow = FollowUtils.getAllFollow(this);
        responses.clear();
        followAdapter.notifyDataSetChanged();
        if (!TextUtils.isEmpty(follow)) {
            mPresenter.getUserInfo("?uid=" + follow);
        }
    }

    private void initListener() {
        imgClose.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                onBackPressed();
            }
        });

        reFollow.addOnScrollListener(new RecyclerView.OnScrollListener() {
            @Override
            public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
                super.onScrolled(recyclerView, dx, dy);
                if (recyclerView.computeVerticalScrollOffset() > 0) {
                    viewDivider.setVisibility(View.VISIBLE);
                } else {
                    viewDivider.setVisibility(View.GONE);
                }
            }
        });
    }

    private void initView() {

        tvTitle = (TextView) findViewById(R.id.tv_title);
        viewDivider = findViewById(R.id.view_divider);
        imgClose = (ImageView) findViewById(R.id.img_close);
        reFollow = (RecyclerView) findViewById(R.id.re_follow);
        reFollow.setLayoutManager(new GridLayoutManager(this, 2));
        reFollow.setItemAnimator(new DefaultItemAnimator());
        responses = new ArrayList<>();
        followAdapter = new FollowAdapter(this, responses);
        reFollow.setAdapter(followAdapter);

    }

    @Override
    public void setData(Object o, @Nullable int id) {
        switch (id) {
            case 0:
                Type type = new TypeToken<List<FollowUserResponse>>() {
                }.getType();
                List<FollowUserResponse> reses = new Gson().fromJson(String.valueOf(o), type);
                responses.clear();
                responses.addAll(reses);
                followAdapter.notifyDataSetChanged();
                break;
        }
    }
}
